package cn.hackcloud.mutithread.mutilthread.stack2;

import java.util.ArrayList;
import java.util.List;

public class MyStack {
	private List<String> list = new ArrayList<String>();

	public synchronized void push() {
		try {
			while (list.size() == 1) {
				this.wait();
			}
			list.add("test:" + Math.random());
			this.notifyAll();
			System.out.println("push=" + list.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public synchronized String pop() {
		String value = "";
		try {
			while (list.size() == 0) {
				System.out.println("pop:" + Thread.currentThread().getName() + " wait");
				this.wait();
			}
			value = "" + list.get(0);
			list.remove(0);
			this.notifyAll();
			System.out.println("pop=" + list.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}
}
